// 章
const {
    getChapterById,   // 根据id 查询章
    getChapterList,   //  查询章列表
    createChapter,   // 添加章
    editChapter,   // 编辑章
    removeChapter,   // 删除章
    getChapterAll,  //获取所有章节
} = require("../../service/catalogue/chapter")

exports.chapterApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/");
    let urlKey = url[2].split("?")[0]
    const { id } = ctx.params || "";
    const { pageNum, pageSize } = ctx.query || "";
    const { title, updator,catalogue_id, creator } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (urlKey == "byid") {
            res = await getChapterById({ id, pageNum, pageSize, catalogue_id })
        } else if (urlKey == "list") {
            res = await getChapterList({ pageNum, pageSize })
        }else if(urlKey == "all"){
            res = await getChapterAll()
        }; break;
        case "POST": res = await createChapter({ title, creator, catalogue_id }); break;
        case "DELETE": res = await removeChapter({ id }); break;
        case "PUT": res = await editChapter({ id, title, updator, catalogue_id }); break;
        default: res = { msg: "操作失败", code: "400" }
    }
    ctx.body = res
}